<?php
/**
 * Created by PhpStorm.
 * User: 小蛮哼哼哼
 * Email: 243194993@qq.com
 * Date: 2021/11/10
 * Time: 15:07
 * motto: 现在的努力是为了小时候吹过的牛逼！
 */

declare(strict_types=1);

namespace app\admin\controller;

use app\common\business\Menu as BusObj;
use app\common\lib\Show;
use app\common\lib\Status;
use think\App;
use think\Exception;
use think\facade\Log;

class Menu extends Base
{
    protected BusObj $busObj;

    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->busObj = new BusObj();
    }
    public function index()
    {
        //获取菜单$this->busObj->getSystemInit();
        return parent::index(); // TODO: Change the autogenerated stub
    }

    public function add()
    {
        //查询是否是首页
        $id = $this->busObj->getMenuByHomeId(Status::HOME_PID);
        $this->assign('id', $id ?? 0);
        //查询菜单
        try {
            $pidMenuList = $this->busObj->getMenuByListInfo();
        } catch (\Exception $e) {
            Log::error($e->getMessage() . " " . $e->getFile() . " " . $e->getLine());
            throw new Exception("查询失败");
        }
        $this->assign('pidMenuList', $pidMenuList);
        return parent::add(); // TODO: Change the autogenerated stub
    }

    public function getMenuTree()
    {
        $menus[] = [
            "value" => 0,
            "name" => "顶级菜单",
            "pid" => 0
        ];
        try {
            $menu = $this->busObj->getMenuByListInfoTree();
        } catch (\Exception $e) {
            return Show::json_show(Status::status_error, "发生错误");
        }
        $result = array_merge($menus,$menu);
        return Show::json_show(Status::status_normal, "ok", $result);
    }
    public function getSystemInitMenu(){
        try {
            $menu = $this->busObj->getSystemInitMenu();
        }catch (\Exception $e){
            return Show::json_show(Status::status_error,"查询错误");
        }
        return Show::json_show(0,"ok",$menu,count($menu));
    }
}